﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Configuration;
using System.IO;
using System.Collections;
using System.Security;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Serialization;
using digitwhu.Helper;

namespace digitwhu.Helper
{
    class LibCrawler
    {
        private String jumpUrl;
        private String searchText;
        public LibCrawler(string searchText)
        {
            this.searchText = searchText;
            //获取跳转参数
            Uri jurl = new Uri("http://opac.lib.whu.edu.cn/F/?func=find-m&find_code=WRD&request=" + searchText + "&pds_handle=GUEST");
            //起始地址
            string jpage = "";
            string j = @"url=(.*?)\/F\/(.*?)\?";// "url=(http:.*?)';";//@"URL=(.*?)F\/(.*?)\?(.*?)>";//"url=(http:.*?)';";//@"URL=(.*?)F\/(.*?)\?(.*?)>";
            HttpWebRequest requestPage = (HttpWebRequest)WebRequest.Create(jurl);
            WebResponse response = requestPage.GetResponse();
            Stream stream = response.GetResponseStream();//获取页面流
            if (response.ContentType.ToLower().StartsWith("text/"))//如果获得成功（即为文本格式）
            {
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);//GetEncoding("GB2312"));//读取获得内容流new Regex("<div class=itemtitle><a href=(.+?)>(.+?)</a>?")
                jpage = reader.ReadToEnd();
                Regex jumpp = new Regex(j);
                Match jumpPage = jumpp.Match(jpage);//进行匹配
                if (jumpPage.Success)
                {
                    Group jumpg = jumpPage.Groups[2];
                    jumpUrl = jumpg.ToString();
                    Console.WriteLine(jumpUrl);
                }

            }
        }

        public String CnCrawler()
        {
            //中文搜索
            Console.WriteLine("The Books in Chinese stack are");
            Uri url = new Uri("http://opac.lib.whu.edu.cn/F/" + jumpUrl + "?func=find-b&find_code=WRD&request=" + this.searchText + "&local_base=WHU01");//将下载地址转换为Uri类型
            // Uri url = new Uri(jumpUrl + "&local_base=WHU01");
            return JsonHelper.JsonSerializer<IList<LibBook>>(Search(url));
        }
        public String WeCrawler()
        {
            //西文搜索
            Console.WriteLine("The Books in West stack are");
            Uri url = new Uri("http://opac.lib.whu.edu.cn/F/" + jumpUrl + "?func=find-b&find_code=WRD&request=" + this.searchText + "&local_base=WHU09");
            // Uri url = new Uri(jumpUrl + "&local_base=WHU09");
            return JsonHelper.JsonSerializer<IList<LibBook>>(Search(url));

        }
        public IList<LibBook> Search(Uri url)
        {
            IList<LibBook> books = new List<LibBook>();
            string BoardStream = "";//下载内容存入此变量

            HttpWebRequest requestPage = (HttpWebRequest)WebRequest.Create(url);
            WebResponse response = requestPage.GetResponse();
            Stream stream = response.GetResponseStream();//获取页面流
            if (response.ContentType.ToLower().StartsWith("text/"))//如果获得成功（即为文本格式）
            {
                string pan = "<div class=itemtitle><a href=(.+?)>(.+?)</a>?";
                string f = "超出集合的记录数";//判断是否所有书籍读取完毕
                string author = "作者：<td class=content>(.*?)<";//作者匹配符
                StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);//GetEncoding("GB2312"));//读取获得内容流new Regex("<div class=itemtitle><a href=(.+?)>(.+?)</a>?")
                BoardStream = reader.ReadToEnd();//将内容流转换为文本并存入变量BoardStream，即为所需要的数据流
                Regex go = new Regex(f);
                Match goNext = go.Match(BoardStream);
                if (goNext.Success)
                {
                    return books;
                }
                Regex r = new Regex(pan);
                Regex a = new Regex(author);
                Match m = r.Match(BoardStream);
                Match searchAuthor = a.Match(BoardStream);//匹配作者
                //int matchCount = 0;
                while (m.Success)
                {
                    LibBook book = new LibBook();

                    for (int l = 1; l <= 2; l++)
                    {
                        book.url = m.Groups[1].ToString();
                        book.title = m.Groups[2].ToString();
                    }
                    book.author = searchAuthor.Groups[1].ToString();//找到作者
                    m = m.NextMatch();
                    searchAuthor = searchAuthor.NextMatch();
                    books.Add(book);
                }
            }
            return books;
        }
    }

    [DataContract]
    class LibBook
    {
        [DataMember]
        public String title;
        [DataMember]
        public String url;
        [DataMember]
        public String author;
    }
}






